package com.lanou3g.musicenjoy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.entity.SearchAlbum;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dllo on 15/10/22.
 */
public class SearchingAlbumAdapter extends BaseAdapter {
    private Context mContext;
    private List<SearchAlbum.DataEntity> data;

    public SearchingAlbumAdapter(Context mContext) {
        this.mContext = mContext;
    }

    //设置数据
    public void setDatas(List<SearchAlbum.DataEntity> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data != null && data.size() > 0 ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null && data.size() > 0 ? data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.searching_album_content, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        SearchAlbum.DataEntity entity = (SearchAlbum.DataEntity) getItem(position);
        if (entity != null) {
            holder.albumName.setText(entity.getName());
            holder.albumSingerName.setText(entity.getSingerName());
            holder.pushingTime.setText(entity.getPublishDate());
            if (entity.getPicUrl() != null) {
                Picasso.with(mContext).load(entity.getPicUrl()).into(holder.albumImg);
            }

        }
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'searching_album_content.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.album_img)
        ImageView albumImg;
        @Bind(R.id.album_name)
        TextView albumName;
        @Bind(R.id.album_singer_name)
        TextView albumSingerName;
        @Bind(R.id.pushing_time)
        TextView pushingTime;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
